--影落日晷
local yingluorigui = fk.CreateSkill {
    name = "mcdh_yingluorigui",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

yingluorigui:addEffect(fk.AfterCardsMove,{
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 then
            local ids = {}
            local room = player.room
            for _, move in ipairs(data) do
                if move.toArea == Card.DiscardPile then
                    if move.moveReason == fk.ReasonDiscard and move.from and move.from ~= player.id then
                        for _, info in ipairs(move.moveInfo) do
                            if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) and
                                    Fk:getCardById(info.cardId).suit == Card.Spade and
                                    room:getCardArea(info.cardId) == Card.DiscardPile then
                                table.insertIfNeed(ids, info.cardId)
                            end
                        end
                    elseif move.moveReason == fk.ReasonJudge then
                        local judge_event = room.logic:getCurrentEvent():findParent(GameEvent.Judge)
                        if judge_event and judge_event.data[1].who ~= player then
                            for _, info in ipairs(move.moveInfo) do
                                if info.fromArea == Card.Processing and Fk:getCardById(info.cardId).suit == Card.Spade and
                                        room:getCardArea(info.cardId) == Card.DiscardPile then
                                    table.insertIfNeed(ids, info.cardId)
                                end
                            end
                        end
                    end
                end
            end
            ids = U.moveCardsHoldingAreaCheck(room, ids)
            if #ids > 0 then
                self.cost_data = ids
                return true
            end
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local ids = table.simpleClone(self.cost_data)
        if #ids > 1 then
            local cards, _ = U.askforChooseCardsAndChoice(player, ids, {"OK"}, self.name,
                    "#yingluorigui-choose", nil, 1, 1)
            if #cards > 0 then
                ids = cards
            end
        end
        room:addPlayerMark(room.current, "@mcdh_yingluorigui-turn", 1)
        room:moveCardTo(ids, Card.PlayerHand, player, fk.ReasonPrey, self.name)
    end,
})

yingluorigui:addEffect('targetmod',{
    residue_func = function(self, player, skill, scope)
        if player:getMark("@mcdh_yingluorigui-turn") ~= 0 and skill.trueName == "slash_skill" and scope == Player.HistoryPhase then
            return player:getMark("@mcdh_yingluorigui-turn")
        end
    end,
})

Fk:loadTranslationTable {
    ["mcdh_yingluorigui"] = "影落日晷",
    ["@mcdh_yingluorigui-turn"] = "影落日晷",
    [":mcdh_yingluorigui"] = "每回合限一次，当其他角色的一张♠牌因判定或弃置而进入弃牌堆时，你可以获得之，然后当前回合角色本回合内使用【杀】的次数上限+1。",
}

return yingluorigui